package io.github.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import io.github.annotation.Log;
import io.github.entity.SysBookEntity;
import io.github.service.SysBookService;
import io.github.util.PageUtils;
import io.github.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: Max
 * @Date: 2019/10/9 11:05
 */

@RestController
@RequestMapping("/admin/sys/book")
public class SysBookController extends AbstractController{

    @Autowired
    SysBookService bookService;

    @RequestMapping("/list")
    public R list(@RequestBody JSONObject jsonObject){
        Integer offset = jsonObject.getInteger("offset");
        Integer limit = jsonObject.getInteger("limit");
        String name = jsonObject.getString("name");
        String orgId = jsonObject.getString("orgId");
        String depId = jsonObject.getString("depId");
        Boolean root = jsonObject.getBoolean("root");

        String number  = jsonObject.getString("number");

        Page<SysBookEntity> pageList = bookService.selectByPage(offset,limit,name,orgId,depId,number,root);
        PageUtils pageUtil = new PageUtils(pageList.getRecords(),pageList.getTotal(),pageList.getSize(),pageList.getCurrent());
        return R.ok().put("data",pageUtil);
    }

    @Log(value = "通讯录添加",action = 1)
    @RequestMapping("/add")
    public R add(@RequestBody SysBookEntity entity){
        bookService.save(entity);
        return R.ok();
    }

    @Log(value = "通讯录更新",action = 2)
    @RequestMapping("/update")
    public R update(@RequestBody SysBookEntity entity){
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("id",entity.getId());
        bookService.update(entity,wrapper);
        return R.ok();
    }

    @Log(value = "通讯录删除",action = 3)
    @RequestMapping("/delete")
    public R delete(@RequestBody JSONObject jsonObject) {
        Integer id = jsonObject.getInteger("ids");
        bookService.deleteById(id);
        return R.ok();
    }

}
